﻿using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;
using SHomeWorkshop.LunarTools;

namespace SHomeWorkshop.LunarMind.Adorners
{
    public class CommentSummaryAdorner : Adorner
    {
        // Be sure to call the base class constructor.
        /// <summary>
        /// 用以表示Comment为概要时画括弧的装饰器。
        /// </summary>
        /// <param name="adornedElement">应传入Comment的“TextBlock”主子元素。</param>
        public CommentSummaryAdorner(UIElement adornedElement, Comment masterComment)
            : base(adornedElement)
        {
            this.masterComment = masterComment;

            //终于解决了选取装饰器中线条宽度为2而不是1像素的问题！！！
            this.SnapsToDevicePixels = false;
            this.SetValue(RenderOptions.EdgeModeProperty, EdgeMode.Unspecified);

            this.ToolTip = tooltipString;

            this.MouseDown += new System.Windows.Input.MouseButtonEventHandler(CommentBorderAdorner_MouseDown);
            this.MouseRightButtonUp += new System.Windows.Input.MouseButtonEventHandler(CommentBorderAdorner_MouseRightButtonUp);
        }

        public static readonly string tooltipString = "可用【边框色】按钮改色";

        void CommentBorderAdorner_MouseRightButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            Comment.S_ContextMenu.IsOpen = true;
            e.Handled = true;
        }

        void CommentBorderAdorner_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (masterComment != null)
            {
                masterComment.SelectOnlySelf();
                e.Handled = true;
            }
        }

        private Comment masterComment;

        public Comment mMasterComment
        {
            get { return masterComment; }
        }

        // A common way to implement an adorner's rendering behavior is to override the OnRender
        // method, which is called by the layout system as part of a rendering pass.
        protected override void OnRender(DrawingContext drawingContext)
        {
            if (masterComment == null) return;

            Rect adornedElementRect = new Rect(this.AdornedElement.RenderSize);

            //先画线。
            Pen linePen = null;

            if (this.masterComment != null && this.masterComment.Editor != null && this.masterComment.MasterTitle != null)
            {
                Brush borderColor = masterComment.BorderColor;

                linePen = new Pen(borderColor, 1);
                linePen.DashStyle = DashStyles.Solid;

                linePen.LineJoin = PenLineJoin.Round;
                linePen.StartLineCap = PenLineCap.Round;
                linePen.EndLineCap = PenLineCap.Round;

                Pen borderPen = new Pen(borderColor, 1);

                if (masterComment.IsSummary)//概要型备注文本块不画边框，而是另画一个反锯齿的括弧装饰器。
                {
                    double height = masterComment.MasterTitle.NeedSize.Height;

                    if (height <= 10) return;

                    PathGeometry geo = new PathGeometry();

                    PathFigure pf = new PathFigure();
                    Point centerStart, centerCtrlLeft, centerCtrlRight,
                        topStart, topCtrlLeft, topCtrlRight,
                        bottomStart, bottomCtrlLeft, bottomCtrlRight;

                    if (masterComment.MasterTitle.Editor.View == ViewMode.HorizontalList ||
                        masterComment.MasterTitle.Editor.View == ViewMode.VerticalList ||       //这两种视图下都只支持右向概要。
                        masterComment.MasterTitle.Direction == TitleDirection.Right)
                    {
                        centerStart = new Point(adornedElementRect.Left - 2, adornedElementRect.Height / 2 + adornedElementRect.Top - 2);
                        centerCtrlLeft = new Point(centerStart.X - 35, centerStart.Y);
                        centerCtrlRight = new Point(centerStart.X - 28, centerStart.Y);

                        double halfHeight = masterComment.MasterTitle.NeedSize.Height / 2;

                        topStart = new Point(centerStart.X - 28, centerStart.Y - halfHeight + 2);
                        topCtrlLeft = new Point(centerStart.X + 7, topStart.Y);
                        topCtrlRight = new Point(centerStart.X + 21, topStart.Y);

                        bottomStart = new Point(topStart.X, centerStart.Y + halfHeight - 2);
                        bottomCtrlLeft = new Point(topCtrlLeft.X, bottomStart.Y);
                        bottomCtrlRight = new Point(topCtrlRight.X, bottomStart.Y);
                    }
                    else
                    {
                        centerStart = new Point(adornedElementRect.Right + 2, adornedElementRect.Height / 2 + adornedElementRect.Top);
                        centerCtrlLeft = new Point(centerStart.X + 28, centerStart.Y);
                        centerCtrlRight = new Point(centerStart.X + 35, centerStart.Y);

                        double halfHeight = masterComment.MasterTitle.NeedSize.Height / 2;

                        topStart = new Point(centerStart.X + 28, centerStart.Y - halfHeight + 2);
                        topCtrlLeft = new Point(centerStart.X - 21, topStart.Y);
                        topCtrlRight = new Point(centerStart.X - 7, topStart.Y);

                        bottomStart = new Point(topStart.X, centerStart.Y + halfHeight - 2);
                        bottomCtrlLeft = new Point(topCtrlLeft.X, bottomStart.Y);
                        bottomCtrlRight = new Point(topCtrlRight.X, bottomStart.Y);
                    }

                    pf.StartPoint = centerStart;

                    pf.Segments.Add(new BezierSegment(centerCtrlLeft, topCtrlLeft, topStart, true));
                    pf.Segments.Add(new BezierSegment(topCtrlRight, centerCtrlRight, centerStart, true));
                    pf.Segments.Add(new BezierSegment(centerCtrlRight, bottomCtrlRight, bottomStart, true));
                    pf.Segments.Add(new BezierSegment(bottomCtrlLeft, centerCtrlLeft, centerStart, true));

                    geo.Figures.Add(pf);

                    drawingContext.DrawGeometry(borderColor, linePen, geo);
                }
            }
        }
    }
}
